package parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import patternsource.IPatternSource;

import datasource.IDataSource;



public class IterationParser implements IParser {
	private List<Map<String, String>> result;

	public List parse(IPatternSource p, IDataSource d) {
		if (p == null || d == null)
			return null;
		result = new ArrayList<Map<String, String>>();
		for (String dataLine : d.getData()) {
			Map<String, String> data = new HashMap<String, String>();
			for (Pattern pattern : p.getPatterns()) {
				Matcher matcher = pattern.matcher(dataLine);
				while (matcher.find())
					data.put(matcher.group(1), matcher.group(2));
			}
			result.add(data);
		}
		return result;
	}

}
